﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Edit
{
    public partial class UpdateStockWarningView : UserControl, IView
    {
        WebRef.VStockThresholdValue itemStockThresholdValue = null;
        Promise _promise = new Promise();
        public UpdateStockWarningView()
        {
            InitializeComponent();
            ValidationManager.Register(this, new Common.Validations.TextEditIsFloatNumber(this.ctlEditWarmingNum, "预警数量", false));
        }
        public UpdateStockWarningView(WebRef.VStockThresholdValue itemStockThresholdValue)
            : this()
        {
            this.itemStockThresholdValue = itemStockThresholdValue;
        }
        #region IView 成员

        public ViewHeader GetHeader()
        {
            return new ViewHeader("修改库存预警值");
        }
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", Resources.isOK, "保存", OnSaveClick))
              .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnCancelClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (this.itemStockThresholdValue != null)
                    {
                        this.ctlEditWareHouse.Text = itemStockThresholdValue.WarehouseName;
                        this.ctlEditMaterial.Text = itemStockThresholdValue.MaterialName;
                        this.ctlEditWarmingNum.EditValue = itemStockThresholdValue.Value;
                    }
                    _promise.Resolve();
                }));
            }).Run();
        }
        public void Resume(ViewResumeArgs args)
        {
        }

        #endregion

        #region Method
        /// <summary>
        /// 库存预警修改
        /// </summary>
        private void UpdateStockWarmingValue()
        {
            WebRef.EStockThresholdValue entity = new WebRef.EStockThresholdValue();
            entity.Id = itemStockThresholdValue.Id;
            entity.WarehouseId = itemStockThresholdValue.WarehouseId;
            entity.MaterialId = itemStockThresholdValue.MaterialId;
            entity.Value = Convert.ToDecimal(this.ctlEditWarmingNum.Text.Trim());

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.UpdateService().UpdateStockThresholdValue(entity);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnSaveClick(object obj1, object obj2)
        {
            string strMess = null;
            if (!ValidationManager.Validate(this, out strMess))
            {
                UIContext.Current.ViewManager.ShowWarning(strMess);
            }
            else
            {
                UpdateStockWarmingValue();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnCancelClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(true, null);
        }
        #endregion
    }
}
